<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="用户信息" field="user_key">
            <a-input v-model="searchForm.user_key" placeholder="请输入用户信息" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户ID" field="uuid">
            <a-input v-model="searchForm.uuid" placeholder="请输入用户ID" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="上级ID" field="community_uuid">
            <a-input v-model="searchForm.community_uuid" placeholder="请输入上级ID" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户状态" field="status">
            <sa-select v-model="searchForm.status" dict="data_type" placeholder="请选择用户状态" allow-clear />
          </a-form-item>
        </a-col>
        
      </template>
      <template #user_type="{ record }">
         <div v-if="record.identity_id==1&&record.is_assistant==2">
          店员
        </div>
        <div style="" v-else>{{ ['','普通用户', '团长', '代理','招商'][record.identity_id] }}</div>
       
      </template>
      <template #nickname="{ record }">
        <div style="display: flex; align-items: center;">
          <div><img :src="record.avatar" alt="" style="border-radius: 100%;width: 40px;height: 40px;margin-right: 5px;"></div>
          <div style="margin-left: 5px; ">
            <div> 用户ID:{{ record.id }}</div>
            <div>用户昵称:{{ record.nickname }}</div>
            <div v-if="record.username">用户姓名:{{ record.username||'--' }}</div>
            <div v-if="record.phone">用户电话:{{ record.phone||'--' }}</div>
            <div>用户等级:{{ record.level_name }}</div>
            <div>用户邀请码:{{ record.inviter_code }}</div>
          </div>
        </div>
      </template>
      <template #form_info="{ record}">
        <div v-if="record.form_info" style="display: flex; align-items: center;">
          <div><img :src="record.form_info?.avatar" alt="" style="border-radius: 100%;width: 40px;height: 40px;margin-right: 5px;"></div>
          <div style="margin-left: 5px;flex: 1; ">
            <div>用户昵称:{{ record.form_info?.nickname || '--' }}</div>
            <div v-if="record.form_info?.username">用户姓名:{{ record.form_info?.username||'--' }}</div>
            <div v-if="record.form_info?.phone">用户电话:{{ record.form_info?.phone||'--' }}</div>
          </div>
        </div>
        <div v-else>--</div>
      </template>
      <template #balance="{ record}">
        <div style="display: flex; align-items: center;">
          <div style="margin-left: 5px;flex: 1; ">
            <div>余额:{{ record.balance1}}</div>
            <div>冻结:{{ record.balance2 }}</div>
            <div>红包:{{ record.balance3}}</div>
          </div>
        </div>
      </template>
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus(1, record.id)"></sa-switch>
      </template>
      <template #is_red="{ record }">
        <sa-switch v-model="record.is_red" @change="changeStatus(2, record.id)"></sa-switch>
      </template>
      <!-- Table 自定义渲染 -->
      
      <template #operationBeforeExtend="{ record }">
        <a-link type="primary" @click="detailAction(record)" @success="refresh"> <icon-eye />详情 </a-link>
      </template>
      <!-- 操作后置扩展 -->
      <template #operationAfterExtend="{ record }">
        <a-dropdown trigger="hover" v-if="!isRecovery" @select="selectOperation($event, record)">
          <a-link><icon-double-right /> 更多</a-link>
          <template #content>
            <a-doption value="recharge">充值</a-doption>
            <a-doption v-if="record.identity_id==1" value="clear">清除团长</a-doption>
            <a-doption value="delete">删除</a-doption>
          </template>
        </a-dropdown>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
    <!-- 充值表单 -->
    <recharge-form ref="rechargeRef" @success="refresh" />
    <!-- 详情 -->
    <detail-form ref="detailRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import DetailForm from './detail.vue'
import RechargeForm from './recharge.vue'
import EditForm from './edit.vue'
import api from '@/api/user'

// 引用定义
const crudRef = ref()
const editRef = ref()
const detailRef = ref()
const rechargeRef = ref()


let isRecovery = computed(() => (crudRef.value ? crudRef.value.isRecovery : false))

// 搜索表单
const searchForm = ref({
  user_key: '',
  uuid: '',
  community_uuid: '',
  status:'',
})

const detailAction = async (record) => {
  detailRef.value?.open(record)
}

const changeStatus = async (type, id) => {
  const response = await api.Operation({ uuid:id, type:type })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

const selectOperation = (value, record) => {
  if (value === 'delete') {
    Modal.info({
      title: '提示',
      content: '确定将该用户删除吗？',
      simple: false,
      onBeforeOk: (done) => {
        changeStatus(4, record.id)
        done(true)
      },
    })
    return
  }
  if (value === 'clear') {
    Modal.info({
      title: '提示',
      content: '确定将该用户清除团长吗？',
      simple: false,
      onBeforeOk: (done) => {
        api.clearCommunityInfo({ uuid: record.id }).then((res) => {
          if (res.code === 200) {
            Message.success(res.message)
            crudRef.value.refresh()
            done(true)
          }
        })
      },
    })
    return
  }
  
  if (value === 'recharge') {
    rechargeRef.value?.open('edit')
    rechargeRef.value?.setFormData(record)
    return
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.listUser,
  rowSelection: { showCheckedAll: true },
  operationColumnWidth: 200,
  add: {
    show: false,
  },
  edit: {
    show: true,
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: false,
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '用户类型', dataIndex: 'user_type', width: 100 },
  { title: '用户信息', dataIndex: 'nickname', width: 250 },
  { title: '上级信息', dataIndex: 'form_info', width: 250 },
  { title: '余额统计', dataIndex: 'balance', width: 120 },
  { title: '用户状态', dataIndex: 'status', type: 'dict', dict: 'data_status', width: 100 },
  { title: '红包设置', dataIndex: 'is_red',type: 'dict', dict: 'data_status', width: 100 },
  { title: '审核状态', dataIndex: 'register_auto',type: 'dict', dict: 'shenhe_status', width: 100 },
  { title: '审核时间', dataIndex: 'register_auto_time', width: 180 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
